/**
 * ztree插件的封装
 */
// 下拉列表树
!function($) {
	
	var $ZTreeSelect = function(id, data) {
		this.id = id;				//触发树菜单id
		this.zNodes = data;			//树数据
		this.settings = null;		//设置配置，可自行配置
		this.treeDivId = "treeModal_"+id;			//树ID
		this.menuContentId = "menuContent_"+id;		//树外容器ID
	};

    $ZTreeSelect.prototype = {
		/**
		 * 初始化ztree的设置
		 */
		initSetting : function() {
			var settings = {
				view : {
					dblClickExpand : false,
					selectedMulti : false,
                    showIcon: false,
                    selectedMulti: false
                },
				data : {simpleData : {enable : true}},
				callback : {
                    beforeClick : this.beforeClick.bind(this)
				}
			};
			return settings;
		},
		
		/**
		 * 手动设置ztree的设置
		 */
		setSettings : function(val) {
			this.settings = val;
		},

		//初始化树的dom
		initDom : function () {
            var treeDom = "<div id='"+this.menuContentId+"' class=\"menuContent\">" +
                "<ul id='"+this.treeDivId+"' class=\"ztree\"></ul>" +
                "</div>";
			$("body").append(treeDom);
        },
		/**
		 * 初始化ztree
		 */
		init : function() {
			var zNodeSeting = null;
			if(this.settings != null){
				zNodeSeting = this.settings;
			}else{
				zNodeSeting = this.initSetting();
			}
			this.initDom();
			this.focusMenuTxt();
			$.fn.zTree.init($("#" + this.treeDivId), zNodeSeting, this.zNodes);
		},
		
		/**
		 * 绑定onclick事件
		 */
        beforeClick : function(e,treeNode) {
            $("#"+this.id).val(treeNode.name);
            $("#"+this.id).blur();
            $("#"+this.id).prev().val(treeNode.id);
            this.hideMenu();
            return false;
		},
        //点击窗口外部，关闭窗口
        onBodyDown : function () {
            if (!(event.target.id === this.id || event.target.id == this.menuContentId || $(event.target).parents("#"+this.menuContentId).length>0)) {
                this.hideMenu();
            }
        },
		//显示树
        showMenuTree : function () {
            var menu = $("#"+this.id);
            var menuOffset = menu.offset();
            $("#"+this.menuContentId).css({left:menuOffset.left + "px", top:menuOffset.top + menu.outerHeight() + "px"}).slideDown("fast");
            $("#"+this.treeDivId).css({width:$("#"+this.id)[0].clientWidth});
            $("body").bind("mousedown", this.onBodyDown.bind(this));
        },
		//隐藏菜单
		hideMenu : function (e) {
        	//自定义关闭菜单树事件
            if (e) e.preventDefault();
            e = $.Event('hide.bs.tree');
            $("#"+this.id).trigger(e);

            $("#"+this.menuContentId).fadeOut("fast");
            $("body").unbind("mousedown", this.onBodyDown);
        },
		//绑定焦点事件,显示菜单树
		focusMenuTxt:function () {
            $("#"+this.id).on("focus",function () {
                this.showMenuTree();
            }.bind(this));
        }
	};

	window.$ZTreeSelect = $ZTreeSelect;
}(jQuery);

//链式调用
!function($) {

    var ZTreeSelect = function(id, data) {
        this.id = id;				//触发树菜单id
        this.zNodes = data;			//树数据
        this.settings = null;		//设置配置，可自行配置
        this.treeDivId="";			//树ID
        this.menuContentId="";		//树外部内容ID
    };

    ZTreeSelect.prototype.constructor = ZTreeSelect;

    ZTreeSelect.prototype = {
        /**
         * 初始化ztree的设置
         */
        initSetting : function() {
            var settings = {
                view : {
                    dblClickExpand : false,
                    selectedMulti : false,
                    showIcon: false,
                    selectedMulti: false
                },
                data : {simpleData : {enable : true}},
                callback : {
                    beforeClick : this.beforeClick.bind(this)
                }
            };
            return settings;
        },

        /**
         * 手动设置ztree的设置
         */
        setSettings : function(val) {
            this.settings = val;
        },

        //初始化树的dom
        initDom : function () {
            //容器ID
            this.menuContentId= "menuContent_"+this.id;
            //树ID
            this.treeDivId = "treeModal_"+this.id;
            var treeDom = "<div id='"+this.menuContentId+"' class=\"menuContent\">" +
                "<ul id='"+this.treeDivId+"' class=\"ztree\"></ul>" +
                "</div>";
            $("body").append(treeDom);
        },
        /**
         * 初始化ztree
         */
        init : function() {
            var zNodeSeting = null;
            if(this.settings != null){
                zNodeSeting = this.settings;
            }else{
                zNodeSeting = this.initSetting();
            }
            this.initDom();
            this.focusMenuTxt();
            $.fn.zTree.init($("#" + this.treeDivId), zNodeSeting, this.zNodes);
        },

        /**
         * 绑定onclick事件
         */
        beforeClick : function(e,treeNode) {
            $("#"+this.id).val(treeNode.name);
            $("#"+this.id).blur();
            $("#"+this.id).prev().val(treeNode.id);
            this.hideMenu();
            return false;
        },
        //点击窗口外部，关闭窗口
        onBodyDown : function () {
            if (!(event.target.id === this.id || event.target.id == this.menuContentId || $(event.target).parents("#"+this.menuContentId).length>0)) {
                this.hideMenu();
            }
        },
        //显示树
        showMenuTree : function () {
            var menu = $("#"+this.id);
            var menuOffset = menu.offset();
            $("#"+this.menuContentId).css({left:menuOffset.left + "px", top:menuOffset.top + menu.outerHeight() + "px"}).slideDown("fast");
            $("#"+this.treeDivId).css({width:$("#"+this.id)[0].clientWidth});
            $("body").bind("mousedown", this.onBodyDown.bind(this));
        },
        //隐藏菜单
        hideMenu : function (e) {
            //自定义关闭菜单树事件
            if (e) e.preventDefault();
            e = $.Event('hide.bs.tree');
            $("#"+this.id).trigger(e);

            $("#"+this.menuContentId).fadeOut("fast");
            $("body").unbind("mousedown", this.onBodyDown);
        },
        //绑定焦点事件,显示菜单树
        focusMenuTxt:function () {
            $("#"+this.id).on("focus",function () {
                this.showMenuTree();
            }.bind(this));
        }
    };

    window.ZTreeSelect = ZTreeSelect;

    // $.fn.ZTreeSelect = ZTreeSelect;
    // $.fn.extend({
    //    ZTreeSelect:ZTreeSelect.init()
    // })

	// $.fn.zTreeSelect.constructor = ZTreeSelect;

}(jQuery);

//左侧菜单树
!function ($) {
	var $ZTreeMenu = function () {
		
    }
}(jQuery);
